package com.foo.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.example.www.FacadeInterfaceServiceStub.GetFacadeGlobalDataFault;
import org.example.www.FacadeInterfaceServiceStub.GlobalFacadeData;
import org.example.www.FacadeWSClient;
import org.example.www.GetFacadeGlobalDataFaultException;

/**
 * Servlet implementation class GlobalFacadeServlet
 */
public class GlobalFacadeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GlobalFacadeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		FacadeWSClient client = new FacadeWSClient();
		GlobalFacadeData result = new GlobalFacadeData();
		RequestDispatcher dispatcher = null;
		try 
		{
			result = client.getAllGlobalData();
		} 
		catch (GetFacadeGlobalDataFaultException e) {
			GetFacadeGlobalDataFault fault = e.getFaultMessage();
			String exceptionString = fault.getGetFacadeGlobalDataFault();
			request.setAttribute("ServerMessage",exceptionString);
			dispatcher = getServletContext().getRequestDispatcher("/global.jsp");
			dispatcher.forward(request, response);
		}
		
		//Add Movies to the array
		session.setAttribute("GlobalMovieData", result.getMovieData().getMovieList());
		session.setAttribute("GlobalEventsArrayData", result.getEventsData().getEventList());
		session.setAttribute("GlobalRestaurantData", result.getRestaurantData().getRestaurantList());
		session.setAttribute("GlobalNewsData", result.getNewsData().getNews_list());
		
		dispatcher = getServletContext().getRequestDispatcher("/global.jsp");
		dispatcher.forward(request, response);
	}

}
